From 33bcccbb5d37971b5d1e3342006e8a1153ed68c1 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Fri, 22 Sep 2017 15:56:47 +0200 Subject: [PATCH] Cleaning lints --- src/cargo/ops/cargo_rustc/custom_build.rs | 2 +- src/cargo/ops/cargo_rustc/mod.rs | 4 +- src/cargo/util/config.rs | 14 +++--- src/cargo/util/dependency_queue.rs | 6 +++ src/cargo/util/errors.rs | 46 +++++++++---------- src/cargo/util/flock.rs | 4 +- src/cargo/util/graph.rs | 10 ++++- src/cargo/util/lazy_cell.rs | 6 +-- src/cargo/util/machine_message.rs | 4 +- src/cargo/util/network.rs | 9 ++-- src/cargo/util/process_builder.rs | 8 ++-- src/cargo/util/profile.rs | 2 +- src/cargo/util/rustc.rs | 2 +- src/cargo/util/toml/mod.rs | 54 +++++++++++------------ src/cargo/util/toml/targets.rs | 44 +++++++++--------- 15 files changed, 112 insertions(+), 103 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index ec78ce4e4..267021ebe 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -270,7 +270,7 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) let library_paths = parsed_output.library_paths.iter().map(|l| { l.display().to_string() }).collect::>(); - machine_message::emit(machine_message::BuildScript { + machine_message::emit(&machine_message::BuildScript { package_id: &id, linked_libs: &parsed_output.library_links, linked_paths: &library_paths, diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 77e4542aa..bc00ccbcd 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -380,7 +380,7 @@ fn rustc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, internal(&format!("compiler produced invalid json: `{}`", line)) })?; - machine_message::emit(machine_message::FromCompiler { + machine_message::emit(&machine_message::FromCompiler { package_id: &package_id, target: &target, message: compiler_message, @@ -525,7 +525,7 @@ fn link_targets<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, } if json_messages { - machine_message::emit(machine_message::Artifact { + machine_message::emit(&machine_message::Artifact { package_id: &package_id, target: &target, profile: &profile, diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index fe15dcb6b..7feb4e1e9 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -386,7 +386,7 @@ impl Config { let cfg_verbose = self.get_bool("term.verbose").unwrap_or(None).map(|v| v.val); let cfg_color = self.get_string("term.color").unwrap_or(None).map(|v| v.val); - let color = color.as_ref().or(cfg_color.as_ref()); + let color = color.as_ref().or_else(|| cfg_color.as_ref()); let verbosity = match (verbose, cfg_verbose, quiet) { (Some(true), _, None) | @@ -449,12 +449,12 @@ impl Config { path.display()) })?; let toml = cargo_toml::parse(&contents, - &path, + path, self).chain_err(|| { format!("could not parse TOML configuration in `{}`", path.display()) })?; - let value = CV::from_toml(&path, toml).chain_err(|| { + let value = CV::from_toml(path, toml).chain_err(|| { format!("failed to load TOML configuration from `{}`", path.display()) })?; @@ -500,12 +500,12 @@ impl Config { }; let registry = cfg.entry("registry".into()) - .or_insert(CV::Table(HashMap::new(), PathBuf::from("."))); + .or_insert_with(|| CV::Table(HashMap::new(), PathBuf::from("."))); match (registry, value) { (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => { let new = mem::replace(new, HashMap::new()); - for (key, value) in new.into_iter() { + for (key, value) in new { old.insert(key, value); } } @@ -535,7 +535,7 @@ impl Config { /// as a path. fn get_tool(&self, tool: &str) -> CargoResult { self.maybe_get_tool(tool) - .map(|t| t.unwrap_or(PathBuf::from(tool))) + .map(|t| t.unwrap_or_else(|| PathBuf::from(tool))) } pub fn jobserver_from_env(&self) -> Option<&jobserver::Client> { @@ -665,7 +665,7 @@ impl ConfigValue { } (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => { let new = mem::replace(new, HashMap::new()); - for (key, value) in new.into_iter() { + for (key, value) in new { match old.entry(key.clone()) { Occupied(mut entry) => { let path = value.definition_path().to_path_buf(); diff --git a/src/cargo/util/dependency_queue.rs b/src/cargo/util/dependency_queue.rs index 1b054428b..efe3cba9d 100644 --- a/src/cargo/util/dependency_queue.rs +++ b/src/cargo/util/dependency_queue.rs @@ -52,6 +52,12 @@ impl Freshness { } } +impl Default for DependencyQueue { + fn default() -> DependencyQueue { + DependencyQueue::new() + } +} + impl DependencyQueue { /// Creates a new dependency queue with 0 packages. pub fn new() -> DependencyQueue { diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index b898baf93..70c501719 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -68,26 +68,26 @@ impl CargoError { } fn is_human(&self) -> bool { - match &self.0 { - &CargoErrorKind::Msg(_) => true, - &CargoErrorKind::TomlSer(_) => true, - &CargoErrorKind::TomlDe(_) => true, - &CargoErrorKind::Curl(_) => true, - &CargoErrorKind::HttpNot200(..) => true, - &CargoErrorKind::ProcessErrorKind(_) => true, - &CargoErrorKind::CrateRegistry(_) => true, - &CargoErrorKind::ParseSemver(_) | - &CargoErrorKind::Semver(_) | - &CargoErrorKind::Ignore(_) | - &CargoErrorKind::Io(_) | - &CargoErrorKind::SerdeJson(_) | - &CargoErrorKind::ParseInt(_) | - &CargoErrorKind::ParseBool(_) | - &CargoErrorKind::Parse(_) | - &CargoErrorKind::Git(_) | - &CargoErrorKind::Internal(_) | - &CargoErrorKind::CargoTestErrorKind(_) | - &CargoErrorKind::__Nonexhaustive { .. } => false + match self.0 { + CargoErrorKind::Msg(_) | + CargoErrorKind::TomlSer(_) | + CargoErrorKind::TomlDe(_) | + CargoErrorKind::Curl(_) | + CargoErrorKind::HttpNot200(..) | + CargoErrorKind::ProcessErrorKind(_) | + CargoErrorKind::CrateRegistry(_) => true, + CargoErrorKind::ParseSemver(_) | + CargoErrorKind::Semver(_) | + CargoErrorKind::Ignore(_) | + CargoErrorKind::Io(_) | + CargoErrorKind::SerdeJson(_) | + CargoErrorKind::ParseInt(_) | + CargoErrorKind::ParseBool(_) | + CargoErrorKind::Parse(_) | + CargoErrorKind::Git(_) | + CargoErrorKind::Internal(_) | + CargoErrorKind::CargoTestErrorKind(_) | + CargoErrorKind::__Nonexhaustive { .. } => false } } } @@ -138,8 +138,8 @@ impl CargoTestError { } pub fn hint(&self) -> String { - match &self.test { - &Test::UnitTest(ref kind, ref name) => { + match self.test { + Test::UnitTest(ref kind, ref name) => { match *kind { TargetKind::Bench => format!("test failed, to rerun pass '--bench {}'", name), TargetKind::Bin => format!("test failed, to rerun pass '--bin {}'", name), @@ -150,7 +150,7 @@ impl CargoTestError { _ => "test failed.".into() } }, - &Test::Doc => "test failed, to rerun pass '--doc'".into(), + Test::Doc => "test failed, to rerun pass '--doc'".into(), _ => "test failed.".into() } } diff --git a/src/cargo/util/flock.rs b/src/cargo/util/flock.rs index 4cdcf5f90..9f6ae48ea 100644 --- a/src/cargo/util/flock.rs +++ b/src/cargo/util/flock.rs @@ -140,7 +140,7 @@ impl Filesystem { /// Handles errors where other Cargo processes are also attempting to /// concurrently create this directory. pub fn create_dir(&self) -> io::Result<()> { - return create_dir_all(&self.root); + create_dir_all(&self.root) } /// Returns an adaptor that can be used to print the path of this @@ -323,7 +323,7 @@ fn acquire(config: &Config, fn create_dir_all(path: &Path) -> io::Result<()> { match create_dir(path) { - Ok(()) => return Ok(()), + Ok(()) => Ok(()), Err(e) => { if e.kind() == io::ErrorKind::NotFound { if let Some(p) = path.parent() { diff --git a/src/cargo/util/graph.rs b/src/cargo/util/graph.rs index 984daa87d..d97b9d44d 100644 --- a/src/cargo/util/graph.rs +++ b/src/cargo/util/graph.rs @@ -56,7 +56,7 @@ impl Graph { marks.insert(node.clone(), Mark::InProgress); - for child in self.nodes[node].iter() { + for child in &self.nodes[node] { self.visit(child, dst, marks); } @@ -69,11 +69,17 @@ impl Graph { } } +impl Default for Graph { + fn default() -> Graph { + Graph::new() + } +} + impl fmt::Debug for Graph { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { writeln!(fmt, "Graph {{")?; - for (n, e) in self.nodes.iter() { + for (n, e) in &self.nodes { writeln!(fmt, " - {}", n)?; for n in e.iter() { diff --git a/src/cargo/util/lazy_cell.rs b/src/cargo/util/lazy_cell.rs index 6a6383907..607f2ef98 100644 --- a/src/cargo/util/lazy_cell.rs +++ b/src/cargo/util/lazy_cell.rs @@ -65,10 +65,8 @@ impl LazyCell { pub fn get_or_try_init(&self, init: F) -> Result<&T, Error> where F: FnOnce() -> Result { - if self.borrow().is_none() { - if self.fill(init()?).is_err() { - unreachable!(); - } + if self.borrow().is_none() && self.fill(init()?).is_err() { + unreachable!(); } Ok(self.borrow().unwrap()) } diff --git a/src/cargo/util/machine_message.rs b/src/cargo/util/machine_message.rs index 5f917f85a..ddfeed7de 100644 --- a/src/cargo/util/machine_message.rs +++ b/src/cargo/util/machine_message.rs @@ -7,8 +7,8 @@ pub trait Message: ser::Serialize { fn reason(&self) -> &str; } -pub fn emit(t: T) { - let mut json: Value = serde_json::to_value(&t).unwrap(); +pub fn emit(t: &T) { + let mut json: Value = serde_json::to_value(t).unwrap(); json["reason"] = json!(t.reason()); println!("{}", json); } diff --git a/src/cargo/util/network.rs b/src/cargo/util/network.rs index 50bb938c3..4c7c4dcb5 100644 --- a/src/cargo/util/network.rs +++ b/src/cargo/util/network.rs @@ -2,11 +2,10 @@ use std; use std::error::Error; use error_chain::ChainedError; - use util::Config; use util::errors::{CargoError, CargoErrorKind, CargoResult}; - use git2; + fn maybe_spurious(err: &E) -> bool where E: ChainedError + 'static { //Error inspection in non-verbose mode requires inspecting the @@ -18,7 +17,7 @@ fn maybe_spurious(err: &E) -> bool //the borrow's actual lifetime for purposes of downcasting and //inspecting the error chain unsafe fn extend_lifetime(r: &Error) -> &(Error + 'static) { - std::mem::transmute::<&Error, &Error>(r) + std::mem::transmute::<&Error, &Error>(r) } for e in err.iter() { @@ -32,7 +31,7 @@ fn maybe_spurious(err: &E) -> bool _ => () } } - &CargoErrorKind::Curl(ref curl_err) + &CargoErrorKind::Curl(ref curl_err) if curl_err.is_couldnt_connect() || curl_err.is_couldnt_resolve_proxy() || curl_err.is_couldnt_resolve_host() || @@ -55,7 +54,7 @@ fn maybe_spurious(err: &E) -> bool /// Retry counts provided by Config object `net.retry`. Config shell outputs /// a warning on per retry. /// -/// Closure must return a CargoResult. +/// Closure must return a `CargoResult`. /// /// # Examples /// diff --git a/src/cargo/util/process_builder.rs b/src/cargo/util/process_builder.rs index 4cb3f88c6..f870c64e1 100644 --- a/src/cargo/util/process_builder.rs +++ b/src/cargo/util/process_builder.rs @@ -24,7 +24,7 @@ impl fmt::Display for ProcessBuilder { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "`{}", self.program.to_string_lossy())?; - for arg in self.args.iter() { + for arg in &self.args { write!(f, " {}", escape(arg.to_string_lossy()))?; } @@ -231,10 +231,10 @@ impl ProcessBuilder { if let Some(cwd) = self.get_cwd() { command.current_dir(cwd); } - for arg in self.args.iter() { + for arg in &self.args { command.arg(arg); } - for (k, v) in self.env.iter() { + for (k, v) in &self.env { match *v { Some(ref v) => { command.env(k, v); } None => { command.env_remove(k); } @@ -248,7 +248,7 @@ impl ProcessBuilder { fn debug_string(&self) -> String { let mut program = format!("{}", self.program.to_string_lossy()); - for arg in self.args.iter() { + for arg in &self.args { program.push(' '); program.push_str(&format!("{}", arg.to_string_lossy())); } diff --git a/src/cargo/util/profile.rs b/src/cargo/util/profile.rs index 5810f5183..da90566f1 100644 --- a/src/cargo/util/profile.rs +++ b/src/cargo/util/profile.rs @@ -37,7 +37,7 @@ impl Drop for Profiler { let start = PROFILE_STACK.with(|stack| stack.borrow_mut().pop().unwrap()); let duration = start.elapsed(); - let duration_ms = duration.as_secs() * 1000 + (duration.subsec_nanos() / 1000000) as u64; + let duration_ms = duration.as_secs() * 1000 + u64::from(duration.subsec_nanos() / 1_000_000); let stack_len = PROFILE_STACK.with(|stack| stack.borrow().len()); if stack_len == 0 { diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index ee68ab769..d1ba345d5 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -29,7 +29,7 @@ impl Rustc { let host = { let triple = verbose_version.lines().find(|l| { l.starts_with("host: ") - }).map(|l| &l[6..]).ok_or(internal("rustc -v didn't have a line for `host:`"))?; + }).map(|l| &l[6..]).ok_or_else(|| internal("rustc -v didn't have a line for `host:`"))?; triple.to_string() }; diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 1fe1771e5..a54c39b3a 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -89,15 +89,15 @@ fn do_read_manifest(contents: &str, Path::Root => {} Path::Seq { parent, index } => { stringify(dst, parent); - if dst.len() > 0 { - dst.push_str("."); + if !dst.is_empty() { + dst.push('.'); } dst.push_str(&index.to_string()); } Path::Map { parent, ref key } => { stringify(dst, parent); - if dst.len() > 0 { - dst.push_str("."); + if !dst.is_empty() { + dst.push('.'); } dst.push_str(key); } @@ -434,7 +434,7 @@ struct Context<'a, 'b> { impl TomlManifest { pub fn prepare_for_publish(&self) -> TomlManifest { let mut package = self.package.as_ref() - .or(self.project.as_ref()) + .or_else(|| self.project.as_ref()) .unwrap() .clone(); package.workspace = None; @@ -449,10 +449,10 @@ impl TomlManifest { bench: self.bench.clone(), dependencies: map_deps(self.dependencies.as_ref()), dev_dependencies: map_deps(self.dev_dependencies.as_ref() - .or(self.dev_dependencies2.as_ref())), + .or_else(|| self.dev_dependencies2.as_ref())), dev_dependencies2: None, build_dependencies: map_deps(self.build_dependencies.as_ref() - .or(self.build_dependencies2.as_ref())), + .or_else(|| self.build_dependencies2.as_ref())), build_dependencies2: None, features: self.features.clone(), target: self.target.as_ref().map(|target_map| { @@ -460,10 +460,10 @@ impl TomlManifest { (k.clone(), TomlPlatform { dependencies: map_deps(v.dependencies.as_ref()), dev_dependencies: map_deps(v.dev_dependencies.as_ref() - .or(v.dev_dependencies2.as_ref())), + .or_else(|| v.dev_dependencies2.as_ref())), dev_dependencies2: None, build_dependencies: map_deps(v.build_dependencies.as_ref() - .or(v.build_dependencies2.as_ref())), + .or_else(|| v.build_dependencies2.as_ref())), build_dependencies2: None, }) }).collect() @@ -577,10 +577,10 @@ impl TomlManifest { process_dependencies(&mut cx, me.dependencies.as_ref(), None)?; let dev_deps = me.dev_dependencies.as_ref() - .or(me.dev_dependencies2.as_ref()); + .or_else(|| me.dev_dependencies2.as_ref()); process_dependencies(&mut cx, dev_deps, Some(Kind::Development))?; let build_deps = me.build_dependencies.as_ref() - .or(me.build_dependencies2.as_ref()); + .or_else(|| me.build_dependencies2.as_ref()); process_dependencies(&mut cx, build_deps, Some(Kind::Build))?; for (name, platform) in me.target.iter().flat_map(|t| t) { @@ -588,10 +588,10 @@ impl TomlManifest { process_dependencies(&mut cx, platform.dependencies.as_ref(), None)?; let build_deps = platform.build_dependencies.as_ref() - .or(platform.build_dependencies2.as_ref()); + .or_else(|| platform.build_dependencies2.as_ref()); process_dependencies(&mut cx, build_deps, Some(Kind::Build))?; let dev_deps = platform.dev_dependencies.as_ref() - .or(platform.dev_dependencies2.as_ref()); + .or_else(|| platform.dev_dependencies2.as_ref()); process_dependencies(&mut cx, dev_deps, Some(Kind::Development))?; } @@ -601,7 +601,7 @@ impl TomlManifest { { let mut names_sources = HashMap::new(); - for dep in deps.iter() { + for dep in &deps { let name = dep.name(); let prev = names_sources.insert(name, dep.source_id()); if prev.is_some() && prev != Some(dep.source_id()) { @@ -612,8 +612,8 @@ impl TomlManifest { } } - let exclude = project.exclude.clone().unwrap_or(Vec::new()); - let include = project.include.clone().unwrap_or(Vec::new()); + let exclude = project.exclude.clone().unwrap_or_default(); + let include = project.include.clone().unwrap_or_default(); let summary = Summary::new(pkgid, deps, me.features.clone() .unwrap_or_else(HashMap::new))?; @@ -622,13 +622,13 @@ impl TomlManifest { homepage: project.homepage.clone(), documentation: project.documentation.clone(), readme: project.readme.clone(), - authors: project.authors.clone().unwrap_or(Vec::new()), + authors: project.authors.clone().unwrap_or_default(), license: project.license.clone(), license_file: project.license_file.clone(), repository: project.repository.clone(), - keywords: project.keywords.clone().unwrap_or(Vec::new()), - categories: project.categories.clone().unwrap_or(Vec::new()), - badges: me.badges.clone().unwrap_or_else(HashMap::new), + keywords: project.keywords.clone().unwrap_or_default(), + categories: project.categories.clone().unwrap_or_default(), + badges: me.badges.clone().unwrap_or_default(), }; let workspace_config = match (me.workspace.as_ref(), @@ -636,7 +636,7 @@ impl TomlManifest { (Some(config), None) => { WorkspaceConfig::Root { members: config.members.clone(), - exclude: config.exclude.clone().unwrap_or(Vec::new()), + exclude: config.exclude.clone().unwrap_or_default(), } } (None, root) => { @@ -651,7 +651,7 @@ impl TomlManifest { let publish = project.publish.unwrap_or(true); let empty = Vec::new(); let cargo_features = me.cargo_features.as_ref().unwrap_or(&empty); - let features = Features::new(&cargo_features, &mut warnings)?; + let features = Features::new(cargo_features, &mut warnings)?; let mut manifest = Manifest::new(summary, targets, exclude, @@ -665,7 +665,7 @@ impl TomlManifest { workspace_config, features, project.im_a_teapot, - me.clone()); + Rc::clone(me)); if project.license_file.is_some() && project.license.is_some() { manifest.add_warning("only one of `license` or \ `license-file` is necessary".to_string()); @@ -730,7 +730,7 @@ impl TomlManifest { Some(ref config) => { WorkspaceConfig::Root { members: config.members.clone(), - exclude: config.exclude.clone().unwrap_or(Vec::new()), + exclude: config.exclude.clone().unwrap_or_default(), } } None => { @@ -857,7 +857,7 @@ impl TomlDependency { (&details.rev, "rev") ]; - for &(key, key_name) in git_only_keys.iter() { + for &(key, key_name) in &git_only_keys { if key.is_some() { let msg = format!("key `{}` is ignored for dependency ({}). \ This will be considered an error in future versions", @@ -924,7 +924,7 @@ impl TomlDependency { } None => Dependency::parse_no_deprecated(name, version, &new_source_id)?, }; - dep.set_features(details.features.unwrap_or(Vec::new())) + dep.set_features(details.features.unwrap_or_default()) .set_default_features(details.default_features .or(details.default_features2) .unwrap_or(true)) @@ -1013,7 +1013,7 @@ impl TomlTarget { } fn crate_types(&self) -> Option<&Vec> { - self.crate_type.as_ref().or(self.crate_type2.as_ref()) + self.crate_type.as_ref().or_else(|| self.crate_type2.as_ref()) } } diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 9fdc9caa7..65393527a 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -80,7 +80,7 @@ fn clean_lib(toml_lib: Option<&TomlLibTarget>, } } Some(TomlTarget { - name: lib.name.clone().or(Some(package_name.to_owned())), + name: lib.name.clone().or_else(|| Some(package_name.to_owned())), ..lib.clone() }) } @@ -98,7 +98,7 @@ fn clean_lib(toml_lib: Option<&TomlLibTarget>, None => return Ok(None) }; - validate_has_name(&lib, "library", "lib")?; + validate_has_name(lib, "library", "lib")?; let path = match (lib.path.as_ref(), inferred) { (Some(path), _) => package_root.join(&path.0), @@ -158,7 +158,7 @@ fn clean_bins(toml_bins: Option<&Vec>, }).collect() }; - for bin in bins.iter() { + for bin in &bins { validate_has_name(bin, "binary", "bin")?; let name = bin.name(); @@ -170,7 +170,7 @@ fn clean_bins(toml_bins: Option<&Vec>, validate_unique_names(&bins, "binary")?; let mut result = Vec::new(); - for bin in bins.iter() { + for bin in &bins { let path = target_path(bin, &inferred, "bin", package_root, &mut |_| { if let Some(legacy_path) = legacy_bin_path(package_root, &bin.name(), has_lib) { warnings.push(format!( @@ -211,7 +211,7 @@ fn clean_bins(toml_bins: Option<&Vec>, if path.exists() { return Some(path); } - return None; + None } } @@ -223,7 +223,7 @@ fn clean_examples(toml_examples: Option<&Vec>, let inferred = infer_from_directory(&package_root.join("examples")); let targets = clean_targets("example", "example", - toml_examples, inferred, + toml_examples, &inferred, package_root, errors)?; let mut result = Vec::new(); @@ -249,7 +249,7 @@ fn clean_tests(toml_tests: Option<&Vec>, let inferred = infer_from_directory(&package_root.join("tests")); let targets = clean_targets("test", "test", - toml_tests, inferred, + toml_tests, &inferred, package_root, errors)?; let mut result = Vec::new(); @@ -282,7 +282,7 @@ fn clean_benches(toml_benches: Option<&Vec>, let inferred = infer_from_directory(&package_root.join("benches")); let targets = clean_targets_with_legacy_path("benchmark", "bench", - toml_benches, inferred, + toml_benches, &inferred, package_root, errors, &mut legacy_bench_path)?; @@ -300,7 +300,7 @@ fn clean_benches(toml_benches: Option<&Vec>, fn clean_targets(target_kind_human: &str, target_kind: &str, toml_targets: Option<&Vec>, - inferred: Vec<(String, PathBuf)>, + inferred: &[(String, PathBuf)], package_root: &Path, errors: &mut Vec) -> CargoResult> { @@ -314,7 +314,7 @@ fn clean_targets(target_kind_human: &str, target_kind: &str, fn clean_targets_with_legacy_path(target_kind_human: &str, target_kind: &str, toml_targets: Option<&Vec>, - inferred: Vec<(String, PathBuf)>, + inferred: &[(String, PathBuf)], package_root: &Path, errors: &mut Vec, legacy_path: &mut FnMut(&TomlTarget) -> Option) @@ -330,14 +330,14 @@ fn clean_targets_with_legacy_path(target_kind_human: &str, target_kind: &str, }).collect() }; - for target in toml_targets.iter() { + for target in &toml_targets { validate_has_name(target, target_kind_human, target_kind)?; } validate_unique_names(&toml_targets, target_kind)?; let mut result = Vec::new(); for target in toml_targets { - let path = target_path(&target, &inferred, target_kind, package_root, legacy_path); + let path = target_path(&target, inferred, target_kind, package_root, legacy_path); let path = match path { Ok(path) => path, Err(e) => { @@ -380,12 +380,12 @@ fn infer_from_directory(directory: &Path) -> Vec<(String, PathBuf)> { entries .filter_map(|e| e.ok()) .filter(is_not_dotfile) - .filter_map(infer_any) + .filter_map(|d| infer_any(&d)) .collect() } -fn infer_any(entry: DirEntry) -> Option<(String, PathBuf)> { +fn infer_any(entry: &DirEntry) -> Option<(String, PathBuf)> { if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") { infer_file(entry) } else if entry.file_type().map(|t| t.is_dir()).ok() == Some(true) { @@ -396,16 +396,16 @@ fn infer_any(entry: DirEntry) -> Option<(String, PathBuf)> { } -fn infer_file(entry: DirEntry) -> Option<(String, PathBuf)> { +fn infer_file(entry: &DirEntry) -> Option<(String, PathBuf)> { let path = entry.path(); path .file_stem() - .and_then(|p| p.to_str()) + .and_then(|p| p.to_str()) .map(|p| (p.to_owned(), path.clone())) } -fn infer_subdirectory(entry: DirEntry) -> Option<(String, PathBuf)> { +fn infer_subdirectory(entry: &DirEntry) -> Option<(String, PathBuf)> { let path = entry.path(); let main = path.join("main.rs"); let name = path.file_name().and_then(|n| n.to_str()); @@ -450,11 +450,11 @@ fn validate_unique_names(targets: &[TomlTarget], target_kind: &str) -> CargoResu fn configure(toml: &TomlTarget, target: &mut Target) { let t2 = target.clone(); - target.set_tested(toml.test.unwrap_or(t2.tested())) - .set_doc(toml.doc.unwrap_or(t2.documented())) - .set_doctest(toml.doctest.unwrap_or(t2.doctested())) - .set_benched(toml.bench.unwrap_or(t2.benched())) - .set_harness(toml.harness.unwrap_or(t2.harness())) + target.set_tested(toml.test.unwrap_or_else(|| t2.tested())) + .set_doc(toml.doc.unwrap_or_else(|| t2.documented())) + .set_doctest(toml.doctest.unwrap_or_else(|| t2.doctested())) + .set_benched(toml.bench.unwrap_or_else(|| t2.benched())) + .set_harness(toml.harness.unwrap_or_else(|| t2.harness())) .set_for_host(match (toml.plugin, toml.proc_macro()) { (None, None) => t2.for_host(), (Some(true), _) | (_, Some(true)) => true, -- 2.30.2